package com.fast.linkbeanadmin.security.impl;

import com.fast.linkbeanadmin.enums.CodeEnum;
import com.fast.linkbeanadmin.pojo.response.ResultBean;
import com.fast.linkbeanadmin.utils.response.ResponseUtil;
import org.springframework.http.MediaType;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author ruan cai yuan
 * @version 1.0
 * @fileName com.fast.linkbeanadmin.security.impl.AuthenticationEntryPointImpl
 * @description: ExceptionTranslationFilter出现AuthenticationException(未登录导致) 或authentication为匿名/记住我时出现的AccessDeniedException 时会回调commence方法
 * @since 2024/8/28 09:44
 */
@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        ResultBean result = ResultBean.error(CodeEnum.UNAUTHORIZED.getCode(), CodeEnum.UNAUTHORIZED.getMsg());
        ResponseUtil.responseData(response, result, MediaType.APPLICATION_JSON_VALUE);
    }
}
